#include "time_check.h"
extern double real_time_adc_data[250];
extern double recording_adc_data[250];
// adc_voltage用于存储ADC单次采样结果
extern double adc_voltage[3];
char call_recording = 0;
double read[2] = {0, 0};
int Counter = 39;
rt_uint32_t personal_clock_tick = 0;
/* 定义线程控制块 */
static struct rt_thread time_check;

/* 定义线程控栈时要求RT_ALIGN_SIZE个字节对齐 */
ALIGN(RT_ALIGN_SIZE)
/* 定义线程栈 */
static rt_uint8_t rt_time_check_stack[1024];
void time_check_init(void *parameter) {

  while (1) {
    __HAL_TIM_SET_AUTORELOAD(&htim3, Counter); //改频率Counter
    //归零前进行一次相关性检测
    read[1] = read[0];
    read[0] = fabs(same_check_once()); //将负相关取绝对值
    read[1] = (read[1] + read[0]) / 2;
    //此处可调节相关性阈值 0~1的范围内
    if (read[0] >= (adc_voltage[0]-(3.3/2)) && read[0] <= 1.1) {
      //关闭干扰
      rt_kprintf("same so close: %.3f\n",(adc_voltage[0]-(3.3/2)));
      HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
    } else {
      if (read[1] > (adc_voltage[0]-(3.3/2))/1.5 && read[1] < 1) {
        //关闭干扰
         rt_kprintf("same so close: %.3f\n",(adc_voltage[0]-(3.3/2))/1.5);
        HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
      }

      // //开启干扰
      // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
      // __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, Counter / 2);
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
    }
    personal_clock_tick = 0;
    // for (int i = 0; i < 100; i++) {
    //   rt_kprintf("adc str %f\t", real_time_adc_data[i]);

    //   // rt_thread_mdelay(10);
    // }
    // rt_kprintf("adc str %f\n", real_time_adc_data[99]);
    // rt_kprintf("\n");
    // memset(recording_adc_data, 0, sizeof(recording_adc_data));
    // rt_kprintf("time_check \r\n");
    if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_11) == GPIO_PIN_SET) {
      call_recording = 1;
      //   HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_7);

      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET);
    }
    if (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_10) == GPIO_PIN_SET) {
      call_recording = 0;
      //   HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_7);
      //开启干扰
      // HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
      // __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, Counter / 2);
      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_SET);
    }

    rt_thread_mdelay(40);
    /* code */
  }
}
void time_check_task(void) {
  rt_thread_init(&time_check,                 /* 线程控制块 */
                 "time_check",                /* 线程名字 */
                 time_check_init,             /* 线程入口函数 */
                 RT_NULL,                     /* 线程入口函数参数 */
                 &rt_time_check_stack[0],     /* 线程栈起始地址 */
                 sizeof(rt_time_check_stack), /* 线程栈大小 */
                 2,                           /* 线程的优先级 */
                 100);                        /* 线程时间片 */
  rt_thread_startup(&time_check);             /* 启动线程，开启调度 */
  rt_kprintf("time_check running\r\n");
}